package com.sunwayworld.basemodule.common.errcode;

import com.sunwayworld.basemodule.common.errcode.code.Code;
import com.sunwayworld.basemodule.common.errcode.code.LimsCode;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

//register，校验全局code唯一
@Service
public class ErrCodeManager implements ApplicationListener<ApplicationReadyEvent> {

    public static final Map<String,String> globalCodes = new HashMap<>();

    private static void register(Code code){
        String globalCode = code.getGlobalCode();
        if(globalCodes.containsKey(globalCode)){
            throw new RuntimeException(globalCode+"存在重复！");
        }
        globalCodes.put(globalCode,code.getGlobalName());
    }

    @Override
    public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
        LimsCode[] codes = LimsCode.values();
        for (LimsCode code : codes) {
            register(code);
        }
    }

}
